package com.yida.gwj.deposit.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.domain.GwjFinanceAccountMain;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountDetailRequest;
import com.yida.gwj.api.domain.request.RemoteGwjFinanceAccountMainRequest;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.api.service.RemoteGwjFinanceService;
import com.yida.gwj.common.domain.dto.GwjBusinessFlagDTO;
import com.yida.gwj.common.domain.dto.GwjBusinessStepDTO;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.utils.GwjConvertUtil;
import com.yida.gwj.deposit.domain.GwjDepositBusiness;
import com.yida.gwj.deposit.domain.GwjDepositBusinessLog;
import com.yida.gwj.deposit.domain.dto.GwjBusinessLogDTO;
import com.yida.gwj.deposit.domain.dto.GwjFinanceAccountDetailsDTO;
import com.yida.gwj.deposit.domain.dto.refund.GwjRefundCompleteBusinessDTO;
import com.yida.gwj.deposit.domain.dto.refund.GwjRefundCompleteCommunityDetailDTO;
import com.yida.gwj.deposit.domain.dto.refund.GwjRefundCompleteMoneyDTO;
import com.yida.gwj.deposit.domain.request.GwjDepositBusinessBuildRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteBusinessQueryRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteCommunityRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteDelBuildRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteDelHouseRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteImportBuildsRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteImportHousesRequest;
import com.yida.gwj.deposit.domain.request.refund.GwjRefundCompleteSaveSubmitRequest;
import com.yida.gwj.deposit.service.IGwjDepositBusinessLogService;
import com.yida.gwj.deposit.service.IGwjDepositBusinessService;
import com.yida.gwj.deposit.service.IGwjRefundCompleteBusinessService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 业务（退多补差管理）Controller
 *
 * @Author: PPV
 * @Date: 2023-02-13
 */
@RestController
@RequestMapping("/refundComplete")
@Api(tags = "退多补差")
public class GwjRefundCompleteBusinessController extends BaseController {

    @Resource
    private RemoteGwjConfigService remoteGwjConfigService;

    @Resource
    private IGwjDepositBusinessLogService gwjDepositBusinessLogService;

    @Resource
    private IGwjDepositBusinessService gwjDepositBusinessService;

    @Resource
    private IGwjRefundCompleteBusinessService gwjRefundCompleteBusinessService;

    @Resource
    private RemoteGwjFinanceService remoteGwjFinanceService;

    /**
     * 查询业务（退多补差管理）列表
     */
    @GetMapping("/list")
    @RequiresPermissions("gwj:deposit:refundComplete:list")
    @ApiOperation("查询业务列表")
    public TableDataInfo list(GwjRefundCompleteBusinessQueryRequest request) {
        return GwjConvertUtil.convertTable(getDataTable(gwjRefundCompleteBusinessService.getPage(request)), GwjRefundCompleteBusinessDTO.class);
    }

    /**
     * 查询业务流程步骤
     */
    @GetMapping("/info")
    @RequiresPermissions("gwj:deposit:refundComplete:list")
    @ApiOperation("查询业务流程步骤")
    public AjaxResult getBusinessInfo() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("bizFlag", GwjConvertUtil.convert(remoteGwjConfigService.selectGwjConfBusinessFlagByBizFlagId(GwjBusinessFlagType.REFUND_COMPLETE.getCode(), SecurityConstants.INNER).getData(), GwjBusinessFlagDTO.class));
        List<GwjBusinessStepDTO> steps = GwjConvertUtil.convertList(remoteGwjConfigService.selectGwjConfStepListByFlagId(GwjBusinessFlagType.REFUND_COMPLETE.getCode(), SecurityConstants.INNER).getData(), GwjBusinessStepDTO.class);
        steps.forEach(stepDTO -> stepDTO.setConfigs(remoteGwjConfigService.selectGwjConfStepConfigInfoCodeListByBizFlagId(stepDTO.getStepId(), SecurityConstants.INNER).getData()));
        ajax.put("steps", steps);
        return ajax;
    }

    /**
     * 导出业务（退多补差管理）列表
     */
    @PostMapping("/export")
    @RequiresPermissions("gwj:deposit:refundComplete:export")
    @Log(title = "退多补差管理-导出（缴存业务管理模块）", businessType = BusinessType.EXPORT, systemName = "gwj")
    @ApiOperation("导出业务（退多补差管理）列表")
    public void export(HttpServletResponse response, GwjDepositBusiness gwjDepositBusiness) {
        gwjDepositBusiness.setBizFlagId(GwjBusinessFlagType.REFUND_COMPLETE.getCode());
        List<GwjDepositBusiness> list = gwjDepositBusinessService.selectGwjDepositBusinessList(gwjDepositBusiness);
        List<GwjRefundCompleteBusinessDTO> resultList = GwjConvertUtil.convertList(list, GwjRefundCompleteBusinessDTO.class);
        ExcelUtil<GwjRefundCompleteBusinessDTO> util = new ExcelUtil<>(GwjRefundCompleteBusinessDTO.class);
        util.exportExcel(response, resultList, "业务（退多补差管理）数据");
    }

    /**
     * 送件
     */
    @PostMapping("/send")
    @RequiresPermissions("gwj:deposit:refundComplete:send")
    @Log(title = "退多补差管理-送件（缴存业务管理模块）", systemName = "gwj")
    @ApiOperation("送件")
    public AjaxResult sendBusiness(@Validated(GwjDepositBusinessLog.Send.class) @RequestBody GwjDepositBusinessLog businessLog) {
        gwjDepositBusinessService.send(businessLog);
        return AjaxResult.success();
    }

    /**
     * 查看业务流程
     */
    @GetMapping("/flow")
    @RequiresPermissions("gwj:deposit:refundComplete:log")
    @ApiOperation("查看业务流程")
    public AjaxResult flow(Long bizId) {
        return AjaxResult.success(gwjDepositBusinessService.getBusinessFlow(bizId, GwjBusinessFlagType.REFUND_COMPLETE.getCode()));
    }

    /**
     * 签署意见
     */
    @PostMapping("/signOpinion")
    @RequiresPermissions("gwj:deposit:refundComplete:signOpinion")
    @Log(title = "退多补差管理-签署意见（缴存业务管理模块）", systemName = "gwj")
    @ApiOperation("签署意见")
    public AjaxResult signOpinion(@Validated(GwjDepositBusinessLog.Sign.class) @RequestBody GwjDepositBusinessLog businessLog) {
        gwjDepositBusinessService.signOpinion(businessLog);
        return AjaxResult.success();
    }

    /**
     * 回退
     */
    @DeleteMapping("/fallback")
    @RequiresPermissions("gwj:deposit:refundComplete:fallback")
    @Log(title = "退多补差管理-回退（缴存业务管理模块）", systemName = "gwj")
    @ApiOperation("回退")
    public AjaxResult fallback(@Validated(GwjDepositBusinessLog.Fallback.class) @RequestBody GwjDepositBusinessLog businessLog) {
        gwjRefundCompleteBusinessService.fallback(businessLog);
        return AjaxResult.success();
    }

    /**
     * 查看业务日志
     */
    @GetMapping("/log/{bizId}")
    @RequiresPermissions("gwj:deposit:refundComplete:log")
    @ApiOperation("查看业务日志")
    public AjaxResult log(@PathVariable Long bizId) {
        return AjaxResult.success(GwjConvertUtil.convertList(gwjDepositBusinessLogService.selectGwjDepositBusinessLogListByBizId(bizId), GwjBusinessLogDTO.class));
    }

    /**
     * 业务作废
     */
    @DeleteMapping("/invalidBusiness")
    @Log(title = "退多补差管理-业务作废（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:invalid")
    @ApiOperation("业务作废")
    public AjaxResult invalidBusiness(@Validated(GwjDepositBusinessLog.Cancellation.class) @RequestBody GwjDepositBusinessLog gwjDepositBusinessLog) {
        gwjDepositBusinessService.invalidBusiness(gwjDepositBusinessLog);
        return AjaxResult.success();
    }

    /**
     * 查询案例信息
     */
    @GetMapping("/caseInfo/{bizId}")
    @RequiresPermissions("gwj:deposit:refundComplete:list")
    @ApiOperation(value = "查看案例信息", response = GwjDepositBusiness.class)
    public AjaxResult caseInfo(@PathVariable Long bizId) {
        return AjaxResult.success(gwjDepositBusinessService.selectGwjDepositBusinessByBizId(bizId));
    }

    /**
     * 查询缴费信息
     */
    @GetMapping("/moneyInfo/{bizId}")
    @RequiresPermissions("gwj:deposit:refundComplete:list")
    @ApiOperation(value = "查询缴费信息", response = GwjRefundCompleteMoneyDTO.class)
    public AjaxResult moneyInfo(@PathVariable Long bizId) {
        return AjaxResult.success(gwjRefundCompleteBusinessService.getMoneyInfo(bizId));
    }

    /**
     * 查询小区信息
     */
    @GetMapping("/communityInfo/{bizId}")
    @RequiresPermissions("gwj:deposit:refundComplete:list")
    @ApiOperation(value = "查询小区信息", response = GwjRefundCompleteCommunityDetailDTO.class)
    public AjaxResult communityInfo(@PathVariable Long bizId) {
        return AjaxResult.success(gwjRefundCompleteBusinessService.getCommunityInfo(bizId));
    }

    /**
     * 导入小区（切换小区）
     */
    @PostMapping("/importCommunity")
    @Log(title = "退多补差管理-导入小区（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:import")
    @ApiOperation("导入小区")
    public AjaxResult importCommunity(@RequestBody @Validated GwjRefundCompleteCommunityRequest request) {
        gwjRefundCompleteBusinessService.importCommunity(request);
        return AjaxResult.success();
    }

    /**
     * 导入楼栋
     */
    @PostMapping("/importBuilds")
    @Log(title = "退多补差管理-导入楼栋（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:import")
    @ApiOperation("导入楼栋")
    public AjaxResult importBuilds(@RequestBody @Validated GwjRefundCompleteImportBuildsRequest request) {
        gwjRefundCompleteBusinessService.importBuilds(request);
        return AjaxResult.success();
    }

    /**
     * 删除楼栋
     */
    @DeleteMapping("/deleteBuild")
    @Log(title = "退多补差管理-删除楼栋（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:delete")
    @ApiOperation("删除楼栋")
    public AjaxResult delBuild(@Validated @RequestBody GwjRefundCompleteDelBuildRequest delBuildRequest) {
        gwjRefundCompleteBusinessService.delBuild(delBuildRequest);
        return AjaxResult.success();
    }

    /**
     * 导入房屋
     */
    @PostMapping("/importHouses")
    @Log(title = "退多补差管理-导入房屋（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:import")
    @ApiOperation("导入房屋")
    public AjaxResult importHouses(@RequestBody @Validated GwjRefundCompleteImportHousesRequest request) {
        gwjRefundCompleteBusinessService.importHouses(request);
        return AjaxResult.success();
    }

    /**
     * 删除房屋
     */
    @DeleteMapping("/deleteHouse")
    @Log(title = "退多补差管理-删除房屋（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:delete")
    @ApiOperation("删除房屋")
    public AjaxResult delHouse(@Validated @RequestBody GwjRefundCompleteDelHouseRequest delHouseRequest) {
        gwjRefundCompleteBusinessService.delHouse(delHouseRequest);
        return AjaxResult.success();
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("gwj:deposit:refundComplete:save")
    @Log(title = "退多补差管理-保存（缴存业务管理模块）", systemName = "gwj")
    @ApiOperation("保存")
    public AjaxResult save(@RequestBody @Validated GwjRefundCompleteSaveSubmitRequest saveSubmitRequest) {
        return AjaxResult.success(gwjRefundCompleteBusinessService.saveBusiness(saveSubmitRequest));
    }

    /**
     * 二次保存
     */
    @PutMapping("/update")
    @RequiresPermissions("gwj:deposit:refundComplete:save")
    @Log(title = "退多补差管理-二次保存（缴存业务管理模块）", systemName = "gwj")
    @ApiOperation("二次保存")
    public AjaxResult update(@Validated @RequestBody GwjRefundCompleteSaveSubmitRequest saveSubmitRequest) {
        gwjRefundCompleteBusinessService.updateBusiness(saveSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 删页
     */
    @DeleteMapping("/deleteBusinessData/{bizId}")
    @Log(title = "退多补差管理-删页（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:delete")
    @ApiOperation("删页")
    public AjaxResult deleteBusinessData(@PathVariable Long bizId) {
        gwjRefundCompleteBusinessService.deleteBusinessData(bizId);
        return AjaxResult.success();
    }

    /**
     * 删除缴存明细
     */
    @DeleteMapping("/delMoneyDetail/{bizId}/{depositDetailId}")
    @Log(title = "退多补差管理-删除缴存明细", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:delete")
    @ApiOperation("删除缴存明细")
    public AjaxResult delMoneyDetail(@PathVariable("bizId") Long bizId, @PathVariable("depositDetailId") Long depositDetailId) {
        gwjRefundCompleteBusinessService.delMoneyDetail(bizId, depositDetailId);
        return AjaxResult.success();
    }

    /**
     * 提交
     */
    @PostMapping("/submit")
    @RequiresPermissions("gwj:deposit:refundComplete:submit")
    @Log(title = "退多补差管理-提交（缴存业务管理模块）", systemName = "gwj")
    @ApiOperation("提交")
    public AjaxResult submit(@Validated @RequestBody GwjRefundCompleteSaveSubmitRequest saveSubmitRequest) {
        gwjRefundCompleteBusinessService.submitBusiness(saveSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 生成财务流水
     */
    @PostMapping("/genFinanceStatement/{bizId}")
    @Log(title = "退多补差管理-退多补差-生成财务流水（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:generate")
    @ApiOperation("生成业务流水")
    public AjaxResult genFinanceStatement(@PathVariable Long bizId) {
        gwjRefundCompleteBusinessService.genFinancialStatement(bizId);
        return AjaxResult.success();
    }

    /**
     * 财务对账
     */
    @PostMapping("/financeCheck/{bizId}")
    @Log(title = "退多补差管理（初始化）-退多补差-财务对账（缴存业务管理模块）", systemName = "gwj")
    @RequiresPermissions("gwj:deposit:refundComplete:check")
    @ApiOperation("财务对账")
    public AjaxResult financeCheck(@PathVariable Long bizId) {
        gwjRefundCompleteBusinessService.financeCheck(bizId);
        return AjaxResult.success();
    }

    /**
     * 主页提交
     *
     * @param businessLog
     * @return
     */
    @RequiresPermissions("gwj:deposit:refundComplete:submit")
    @PostMapping("/indexSubmit")
    @ApiOperation(value = "主页提交")
    @Log(title = "退多补差-主页提交（退多补差业务管理模块）", systemName = "gwj")
    public AjaxResult indexSubmit(@RequestBody @Validated GwjDepositBusinessLog businessLog) {
        gwjRefundCompleteBusinessService.indexSubmit(businessLog);
        return AjaxResult.success();
    }

    /**
     * 查询业务（业主缴存）总账详情
     * 
     * @param mainRequest 查询参数
     * @return 总账详情对象
     */
    @GetMapping("/accountMain/list")
    @ApiOperation(value = "查询总账", response = GwjFinanceAccountMain.class)
    public TableDataInfo getAccountMain(@Validated RemoteGwjFinanceAccountMainRequest mainRequest) {
        startPage();
        return getDataTable(gwjRefundCompleteBusinessService.selectGwjFinanceAccountMainList(mainRequest));
    }

    /**
     * 查询业务（业主缴存）分账详情
     * 
     * @param detailRequest 分账查询参数
     * @return 分账详情对象
     */
    @GetMapping("/accountDetail/list")
    @ApiOperation(value = "查询分账", response = GwjFinanceAccountDetailsDTO.class)
    public TableDataInfo getAccountDetail(@Validated RemoteGwjFinanceAccountDetailRequest detailRequest) {
        startPage();
        return getDataTable(gwjRefundCompleteBusinessService.selectGwjFinanceAccountDetailList(detailRequest));
    }

    @PostMapping("/updateBuildInfo")
    @ApiOperation(value = "修改楼栋信息", response = AjaxResult.class)
    public AjaxResult updateBuildInfo(@RequestBody GwjDepositBusinessBuildRequest request) {
        gwjRefundCompleteBusinessService.updateBuildInfo(request);
        return AjaxResult.success("修改成功！");
    }

}
